package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetMaterialCategoryRes;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class AdCommonMaterialActivity extends BaseActivity {

    private ImageView back;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Integer> idList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();
    private List<Fragment> viewList = new ArrayList<>();
    private List<GetMaterialCategoryRes.DataBean> adDatas = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ad_common_material;
    }

    @Override
    protected void initView() {
        back = (ImageView) findViewById(R.id.back);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    @Override
    protected void initData() {
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setTabTextColors(getResources().getColor(R.color.black), getResources().getColor(R.color.new_red));
        getMaterialCategory();
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
                getResources().getDisplayMetrics());

        viewPager.setOffscreenPageLimit(1);
        viewPager.setPageMargin(pageMargin);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();//
        JAnalyticsInterface.onPageStart(this,"通用素材");
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this,"通用素材");
    }

    private void getMaterialCategory() {
        UserServiceImpl.getMaterialCategory(new NetworkSubscriber<GetMaterialCategoryRes>() {
            @Override
            public int onSuccess(GetMaterialCategoryRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    adDatas.clear();
                    adDatas.addAll(data.getData());
                    titleList.clear();
                    idList.clear();

                    for (GetMaterialCategoryRes.DataBean bean : adDatas) {
                        idList.add(bean.getCategory_id());
                        titleList.add(bean.getCategory_name());
                    }

                    tabLayout.removeAllTabs();
                    for (int i = 0; i < titleList.size(); i++) {
                        tabLayout.addTab(tabLayout.newTab().setText(titleList.get(i)));
                    }

                    viewList.clear();
                    for (int i = 0; i < idList.size(); i++) {
                        AdCommonMaterialsFragment fragment = new AdCommonMaterialsFragment();
                        Bundle bundle = new Bundle();
                        bundle.putInt("material_id", idList.get(i));
                        bundle.putString("titleName",titleList.get(i));
                        fragment.setArguments(bundle);
                        viewList.add(fragment);
                    }

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    HomePagerAdapter homePagerAdapter = new HomePagerAdapter(fragmentManager, viewList, titleList, viewPager);
                    viewPager.setAdapter(homePagerAdapter);
                    tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
                    tabLayout.setTabsFromPagerAdapter(homePagerAdapter);//给Tabs设置适配器
                } else {
                    ToastUtils.show(AdCommonMaterialActivity.this, data.getMsg());
                }
                return 0;
            }
        });
    }


}
